<?php

namespace tlan_turing\migration\command\migrate;

use think\console\Input;
use think\console\input\Argument as InputArgument;
use think\console\Output;
use tlan_turing\migration\Command;

class Create extends Command
{
    /**
     * {@inheritdoc}
     */
    protected function configure()
    {
        $this->setName('migrate:create')
            ->setDescription('Create a new migration')
            ->addArgument('name', InputArgument::REQUIRED, '创建在应用中使用 adminapi@UserName 创建在根目录不需要@')
            ->setHelp(sprintf('%sCreates a new database migration%s', PHP_EOL, PHP_EOL));
    }

    /**
     * Create the new migration.
     *
     * @param Input  $input
     * @param Output $output
     * @return void
     * @throws InvalidArgumentException
     * @throws RuntimeException
     */
    protected function execute(Input $input, Output $output)
    {
        /** @var Creator $creator */
        $creator = $this->app->get('migration.creator');

        $className  = trim($input->getArgument('name'));

//        $className = $this->getClassName($className);


        if (str_contains($className, '@')) {
            [$app, $name] = explode('@', $className);
        } else {
            $app = '';
            $name = $className;
        }

        $path = $creator->create($name , $app);

        $output->writeln('<info>created</info> .' . str_replace(getcwd(), '', realpath($path)));
    }

    protected function getClassName(string $name): string
    {
        if (str_contains($name, '\\')) {
            return $name;
        }



        if (str_contains($name, '@')) {
            [$app, $name] = explode('@', $name);
        } else {
            $app = '';
        }

        if (str_contains($name, '/')) {
            $name = str_replace('/', '\\', $name);
        }

        return  $name;
    }





}